//
// CCaptureController.cpp
//

/*-----------------------------------------------------*\
			HQ Tech, Make Technology Easy!       
 More information, please go to http://hqtech.nease.net.
/*-----------------------------------------------------*/

#include "stdafx.h"
#include "CCaptureController.h"

#include "GlobalDefs.h"
#include "CDXGraph.h"
#include "CLiveInputFilters.h"
#include "CAviMuxFilter.h"
#include "CFileWriterFilter.h"
#include "CLiveCapture.h"
#include "UDsUtils.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////
CCaptureController::CCaptureController()
{
	mVideoEncoder = NULL;
	mAviMux       = NULL;
	mFileWriter   = NULL;
}

CCaptureController::~CCaptureController()
{
	ReleaseOutputFilters();
}

BOOL CCaptureController::ConnectFilters(void)
{
	BOOL pass = FALSE;

	// Connect video preview
	IPin * pOut = mInputFilters->GetPreviewPin();
	IPin * pIn  = mVideoRenderer->GetInputPin();
	if (pOut && pIn)
	{
		pass = mGraph->ConnectFilters(pOut, pIn);
	}

	// Connect video stream to the mux filter
	// Insert a encoder if necessary
	pOut = mInputFilters->GetVideoOutPin();
	if (pass && mVideoEncoder && mVideoEncoder->GetFilter())
	{
		// Connect to the encoder first
		pIn = mVideoEncoder->GetPin(TRUE);
		if (pOut && pIn)
		{
			pass = mGraph->ConnectFilters(pOut, pIn);
		}
		// Retrieve the output pin of the encoder
		pOut = mVideoEncoder->GetPin(FALSE);
	}

	pIn = mAviMux->GetUnconnectedInputPin();
	if (pass && pOut && pIn)
	{
		pass = mGraph->ConnectFilters(pOut, pIn);
	}

	// Connect audio stream to the mux filter
	pOut = mInputFilters->GetAudioOutPin();
	pIn  = mAviMux->GetUnconnectedInputPin();
	if (pass && pOut && pIn)
	{
		pass = mGraph->ConnectFilters(pOut, pIn);
	}
	
	// Connect the mux filter to the file writer
	pOut = mAviMux->GetOutputPin();
	pIn  = mFileWriter->GetInputPin();
	if (pass && pOut && pIn)
	{
		pass = mGraph->ConnectFilters(pOut, pIn);
	}

	return pass;
}

BOOL CCaptureController::CreateOutputFilters(void)
{
	BOOL pass = CGraphController::CreateOutputFilters();
	if (pass)
	{
		// Create mux filter
		mAviMux = new CAviMuxFilter(mGraph->GetGraph());
		pass = mAviMux->CreateFilter();

		// Create file writer filter
		mFileWriter = new CFileWriterFilter(mGraph->GetGraph());
		pass = mFileWriter->CreateFilter();
		mFileWriter->SetDestFile(mLiveCapture->GetOutputFile());

		// Create encoder filter if necessary
		if (mLiveCapture->GetEncodingType() == ET_DIVX)
		{
			IBaseFilter* pFlt = UDsUtils::CreateCompressor(TRUE, 
				"DivX 5.0.2 Codec");
			
			if (pFlt)
			{
				mVideoEncoder = new CDXFilter(mGraph->GetGraph());
				mVideoEncoder->Attach(pFlt, "DivX Encoder");
				pFlt->Release();
				pass = TRUE;
			}
			else
			{
				pass = FALSE;
				AfxMessageBox("DivX Encoder failed to create!");
			}
		}
	}
	return pass;
}

void CCaptureController::ReleaseOutputFilters(void)
{
	SAFE_DELETE(mVideoEncoder);
	SAFE_DELETE(mAviMux);
	SAFE_DELETE(mFileWriter);

	CGraphController::ReleaseOutputFilters();
}